////////////////////////////////////////////////////////////////////////////////


*This do-file produces all output in Appendix A.


////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
*Figure A.1: UKHLS/BHPS job security questions crosstab, Waves 6 and 7
////////////////////////////////////////////////////////////////////////////////

cd "$ukdata"

use "clean/uk_translation.dta", clear
	drop if ukhls_security<1
	drop if bhps_security<1

corr ukhls_security bhps_security
di "Correlation: `r(rho)'"
	
collapse (mean) ukhls_security if !missing(ukhls_security,bhps_security) ///
[pw=relative_xw], by(bhps_security)

scatter ukhls_security bhps_security, bgcolor(white) xlabel(1/7) graphregion(color(white)) ytitle("Mean UKHLS question response") xtitle("BHPS question response") title("BHPS/UKHLS job security question response" "Waves 6 and 7 crosstab")
graph export "$othergraphics/bhps_ukhls_crosstab.png", as(png) replace

////////////////////////////////////////////////////////////////////////////////
*Figure A.2: Insecure proportion of UK workforce, different imputation methods
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
*BHPS/UKHLS crosswalk with and without covariates
////////////////////////////////////////////////////////////////////////////////

*With covariates
use "clean/uk_translation.dta", clear
	drop if ukhls_security<1
	drop if bhps_security<1

*Imputation
mlogit ukhls_security i.bhps_security age age_squared jobtenure i.male i.temp i.parttime i.union ib1.marital i.uni i.immigrant i.nonwhite ib1.isco if insample [pw=relative_xw], vce(robust) b(2)

*Predict probabilities of UKHLS response for BHPS sample
use "clean/uk_linear_reverse.dta", clear
	drop ukhls1-ukhls4
	predict ukhls* if inrange(wave,1,18)
	gen pred_ukhls_security=ukhls1+2*ukhls2+3*ukhls3+4*ukhls4 if bhps==1

keep if !missing(ukhls1)|(ukhls_security>0&!missing(ukhls_security))|stats==1

drop if inlist(wave,19,21,23,25)
	forval i=1/4 {
		gen ukhls_security`i'=1 if ukhls_security==`i'
		replace ukhls_security`i'=0 if ukhls_security!=.&ukhls_security>=0& ukhls_security!=`i'
		}
		
	forval i=1/4 {
		replace ukhls_security`i'=ukhls`i' if inrange(wave,1,18)& !inlist(wave,6,7)
	}

collapse (mean) ukhls_security1-ukhls_security4 if insample [pw=relative_xw], by(year)

gen insecure_w=ukhls_security1+ukhls_security2
sort year
save "temp/with_covariates.dta", replace

*Without covariates
use "clean/uk_translation.dta", clear
	drop if ukhls_security<1
	drop if bhps_security<1

*Imputation
mlogit ukhls_security i.bhps_security if insample [pw=relative_xw], vce(robust) b(2)

*Predict probabilities of UKHLS response for  BHPS sample
use "clean/uk_linear_reverse.dta", clear
	drop ukhls1-ukhls4
	predict ukhls* if inrange(wave,1,18)
	gen pred_ukhls_security=ukhls1+2*ukhls2+3*ukhls3+4*ukhls4 if bhps==1
	
keep if !missing(ukhls1)|(ukhls_security>0&!missing(ukhls_security))|stats==1

drop if inlist(wave,19,21,23,25)
	forval i=1/4 {
		gen ukhls_security`i'=1 if ukhls_security==`i'
		replace ukhls_security`i'=0 if ukhls_security!=.&ukhls_security>=0&ukhls_security!=`i'
		}
		
	forval i=1/4 {
		replace ukhls_security`i'=ukhls`i' if inrange(wave,1,18)&!inlist(wave,6,7)
	}

collapse (mean) ukhls_security1-ukhls_security4 if insample [pw=relative_xw], by(year)

gen insecure_wo=ukhls_security1+ukhls_security2
sort year

merge 1:1 year using "temp/with_covariates.dta"
	drop if year>2009

scatter insecure_w insecure_wo year, c(l l) msize(0 0) lwidth(medthick medthick) graphregion(color(white)) bgcolor(white) title("Insecure proportion of UK workforce," "different imputation methods") legend(label(1 "With covariates") label(2 "Without covariates")) xlabel(1991(2)2009) xtitle("Year")
graph export "$ukgraphics/crosswalk_methods.png", as(png) replace

cap erase "temp/with_covariates.dta"

////////////////////////////////////////////////////////////////////////////////
*Figures A.3-A.5: Unbinned responses by year
////////////////////////////////////////////////////////////////////////////////

**A.3: UK
cd "$ukdata"

use "clean/uk_linear_reverse.dta", clear
drop if inlist(wave,19,21,23,25)
	forval i=1/4 {
		gen ukhls_security`i'=1 if ukhls_security==`i'
		replace ukhls_security`i'=0 if ukhls_security!=.&ukhls_security>=0& ///
ukhls_security!=`i'
		}
		
	forval i=1/4 {
		replace ukhls_security`i'=ukhls`i' if inrange(wave,1,18)
	}

collapse (mean) ukhls_security1-ukhls_security4 if insample [pw=relative_xw], ///
by(year)

scatter ukhls* year, c(l l l l) msize(0 0 0 0) lwidth(medthick medthick medthick medthick) graphregion(color(white)) bgcolor(white) lcolor(gs11 gs7 gs4 gs0) lpattern(shortdash dash longdash solid) ytitle("Proportion of respondents") xtitle("Year") title("BHPS job security question responses by year") legend(label(1 "1 (Very likely)") label(2 "2 (Likely)") label(3 "3 (Unlikely)") label(4 "4 (Very unlikely)")) note(`"Responses are to the question "How satisfied are you with your job security?""')
graph export "$ukgraphics/uk_nonbinned_responses.png", as(png) replace

**A.4: US (GSS)
cd "$usdata"

use "clean/gss_clean.dta", clear
	forval i=1/4 {
		gen joblose`i'=1 if joblose==`i'
		recode joblose`i' .=0
	}
	
collapse (mean) joblose* if insample [pweight=wtssall], by(year)
drop joblose

gen insecure=joblose1+joblose2

scatter joblose* year, c(l l l l) msize(0 0 0 0) lwidth(medthick medthick medthick medthick) lcolor(gs11 gs7 gs4 gs0) lpattern(shortdash dash longdash solid) graphregion(color(white)) ytitle("Proportion of respondents") xtitle("Year") title("GSS job security question responses by year") bgcolor(white) legend(label(1 "1 (Very likely)") label(2 "2 (Fairly likely)") label(3 "3 (Not too likely)") label(4 "4 (Not likely)")) note(`"Responses are to the question "How likely are you to lose your job in the next 12 months?""')
graph export "$usgraphics/us_nonbinned_responses.png", as(png) replace

**A.5: Germany
cd "$germanydata"

use "clean/soep_clean.dta", clear
	forval i=1/3 {
		gen jobsecurity`i'=1 if jobsecurity==`i'
		recode jobsecurity`i' .=0
	}

collapse (mean) jobsecurity* if insample [pw=xw], by(year)
drop jobsecurity

scatter jobsecurity* year, c(l l l) msize(0 0 0) lwidth(medthick medthick medthick) lcolor(gs11 gs5 gs0) lpattern(dash longdash solid) bgcolor(white) graphregion(color(white)) ytitle("Proportion of respondents") xtitle("Year") title("SOEP job security question responses by year") legend(label(1 "1 (Very concerned)") label(2 "2 (Somewhat concerned)") label(3 "3 (Not concerned at all)")) note(`"Responses are to the question "How concerned are you about your job security?""')

graph export "$germanygraphics/de_nonbinned_responses.png", as(png) replace

////////////////////////////////////////////////////////////////////////////////

*Separation rate plots

////////////////////////////////////////////////////////////////////////////////

**PART ONE: US
cd "$usdata"

use "clean/us_flows.dta", clear
drop if year>2018

*Figure A.6: CPS-derived monthly averages
scatter cps_total_new cps_unemp cps_layoff cps_dropout cps_switcher year if year>=1998, c(l l l l l) msize(0 0 0 0 0) ylabel(0(.01).08) legend(label(1 "Total separation rate") label(2 "Sep.-to-unemp. rate") label(3 "Layoff separation rate") label(4 "Dropout separation rate") label(5 "Job-to-job separation rate")) bgcolor(white) graphregion(color(white)) title("CPS-derived monthly separation rates" "(yearly average)") bgcolor(white) graphregion(color(white))
graph export "$usgraphics/separations_cps.png", as(png) replace

*Figure A.7: Separation rates from different sources (CPS, Shimer, JOLTS)
scatter shimer_total cps_total_new jolts_total cps_layoff jolts_inv year if year>=1978, c(l l l l l) msize(0 0 0 0 0) legend(label(1 "Shimer total") label(2 "CPS total") label(3 "JOLTS total") label(4 "CPS layoff") label(5 "JOLTS involuntary")) ylabel(0(.02).08) title("Separation probabilities") ytitle("Separation probability") xtitle("Year") bgcolor(white) graphregion(color(white))
graph export "$usgraphics/shimer_vs_jolts_vs_cps.png", as(png) replace

*Shimer-CPS correlation
corr shimer_total cps_total_new
di "Correlation==`r(rho)'"

*JOLTS_inv-CPS_inv correlation
corr jolts_inv cps_layoff
di "Correlation==`r(rho)'"

**PART TWO: UK
cd "$ukdata"

use "clean/uk_flows.dta", clear
gen lfs_total = e_u + e_n + e_e
gen lfs_total_invol = e_u*invol_u + e_n*invol_n + e_e*invol_e

*A.8: QLFS-derived separation rates, UK
scatter lfs_total lfs_total_invol e_u e_n e_e year, c(l l l l l) msize(0 0 0 0 0) bgcolor(white) graphregion(color(white)) xtitle("Year") legend(label(1 "Total separation rate") label(2 "Involuntary separation rate") label(3 "Sep.-to-unemp. rate") label(4 "Dropout separation rate") label(5 "Job-to-job separation rate")) title("QLFS-derived quarterly separation rates") xlabel(1991(3)2018, angle(45))
graph export "$ukgraphics/separations_qlfs.png", as(png) replace

**PART THREE: Germany
cd "$germanydata"

use "clean/germany_flows.dta", clear

*A.9: SOEP-derived separation rates
scatter sep_total layoff_total year, c(l l) msize(0 0) title("SOEP-derived separation rates") xlabel(1985(2)2017, angle(45)) ytitle("Separation rate") xtitle("Year") graphregion(color(white)) bgcolor(white) legend(label(1 "Total separations") label(2 "Involuntary separations"))
graph export "$germanygraphics/separations_germany.png", as(png) replace

////////////////////////////////////////////////////////////////////////////////

*Other job security questions in SOEP

////////////////////////////////////////////////////////////////////////////////

**Figure A.10: different measures of job security in Germany
cd "$germanydata"

use "clean/soep_clean.dta", clear

gen insample2=!missing(year, temp, parttime, semp, immigrant, marital, isco, male, age, age_squared, uni, jobtenure, marginal, region, xw)&employed==1&jobtenure>=0&age<=65&(plb0433_v2>=0|plb0433_v1>0)

gen insecure2 = 1 if inrange(plb0433_v1,1,2)
replace insecure2 = 0 if inrange(plb0433_v1,3,4)
rename plb0433_v2 pr_jobloss
replace pr_jobloss = pr_jobloss/100
replace pr_jobloss = . if pr_jobloss<0

collapse pr_jobloss insecure2 if insample2 [pw=xw], by(year)
format pr_jobloss %10.4g

merge 1:m year using "clean/soep_insecure_summary.dta"
sort year

*Figure
graph twoway (scatter insecure year if region==1, c(l) msize(0) lwidth(medthick)) (scatter insecure2 year if region==1, c(l) lwidth(medthick) msize(0)) (scatter pr_jobloss year if region==1, c(l) lwidth(medthick) msize(0)) , graphregion(color(white)) bgcolor(white) title("Job insecurity in Germany, 1985-2018" "(Different insecurity measures)") xtitle("Year") ytitle("Proportion of workers/probability") xlabel(1985(2)2018, angle(45)) legend(label(1 "Proportion that feel" "insecure (variable 1)") label(2 "Proportion that feel" "insecure (variable 2)") label(3 "Probability of job loss"))
graph export "$germanygraphics/different_insecurity_variables.png", as(png) replace

**Regressions with the other questions
cd "$germanydata"

use "clean/soep_clean.dta", clear

gen insample2=!missing(year, temp, parttime, semp, immigrant, marital, isco, male, age, age_squared, uni, jobtenure, marginal, region, xw)&employed==1&jobtenure>=0&age<=65&(plb0433_v2>=0|plb0433_v1>0)

gen insecure2 = 1 if inrange(plb0433_v1,1,2)
replace insecure2 = 0 if inrange(plb0433_v1,3,4)
rename plb0433_v2 pr_jobloss
replace pr_jobloss = pr_jobloss/100
replace pr_jobloss = . if pr_jobloss<0
format pr_jobloss %10.4g

*Year dummies without controls--baseline spec
logit insecure ib1999.year if insample==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_nocontrols.dta, replace)

*Year dummies with controls--baseline spec
logit insecure ib1999.year i.temp i.parttime i.semp i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.marginal i.uni i.ddr if insample==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_controls.dta, replace)
	
*Year dummies without controls--Alternative #1
logit insecure2 ib1998.year if insample2==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_nocontrols_a1.dta, replace)

*Year dummies with controls--Alternative #1
logit insecure2 ib1998.year i.temp i.parttime i.semp i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.marginal i.uni i.ddr if insample2==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_controls_a1.dta, replace)
	
*Year dummies without controls--Alternative #2
reg pr_jobloss ib1999.year if insample2==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_nocontrols_a2.dta, replace)

*Year dummies with controls--Alternative #2
reg pr_jobloss ib1999.year i.temp i.parttime i.semp i.immigrant c.jobtenure ib3.marital ib1.isco i.male age age_squared i.marginal i.uni i.ddr if insample2==1 [pw=xw], vce(cluster year)
	margins, dydx(i.year) saving(temp/soep_controls_a2.dta, replace)
	
use "temp/soep_nocontrols.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx ci_upper ci_lower se)
tempfile nocontrols
save `nocontrols'

use "temp/soep_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `nocontrols'
	rename (_margin _ci_lb _ci_ub _se) (dydx_controls ci_lower_controls ci_upper_controls se_controls)
	keep year dydx ci_upper ci_lower dydx_controls ci_upper_controls ci_lower_controls se_controls se
	set obs 34
	replace dydx=0 in 34
	replace dydx_controls=0 in 34
	replace ci_upper_controls=0 in 34
	replace ci_lower_controls=0 in 34
	replace ci_upper=0 in 34
	replace ci_lower=0 in 34
	replace year=1999 in 34
	sort year
tempfile baseline
save `baseline'

use "temp/soep_nocontrols_a1.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_a1 ci_upper_a1 ci_lower_a1 se_a1)
tempfile nocontrols
save `nocontrols'

use "temp/soep_controls_a1.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `nocontrols'
	rename (_margin _ci_lb _ci_ub _se) (dydx_controls_a1 ci_lower_controls_a1 ci_upper_controls_a1 se_controls_a1)
	keep year dydx_a1 ci_upper_a1 ci_lower_a1 dydx_controls_a1 ci_upper_controls_a1 ///
ci_lower_controls_a1 se_controls_a1 se_a1
	set obs 9
	replace dydx_a1=0 in 9
	replace dydx_controls_a1=0 in 9
	replace ci_upper_controls_a1=0 in 9
	replace ci_lower_controls_a1=0 in 9
	replace ci_upper_a1=0 in 9
	replace ci_lower_a1=0 in 9
	replace year=1998 in 9
	sort year
tempfile a1
save `a1'

use "temp/soep_nocontrols_a2.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_a2 ci_upper_a2 ci_lower_a2 se_a2)
tempfile nocontrols
save `nocontrols'

use "temp/soep_controls_a2.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
merge 1:1 year using `nocontrols'
	rename (_margin _ci_lb _ci_ub _se) (dydx_controls_a2 ci_lower_controls_a2 ci_upper_controls_a2 se_controls_a2)
	keep year dydx_a2 ci_upper_a2 ci_lower_a2 dydx_controls_a2 ci_upper_controls_a2 ci_lower_controls_a2 se_controls_a2 se_a2
	set obs 9
	replace dydx_a2=0 in 9
	replace dydx_controls_a2=0 in 9
	replace ci_upper_controls_a2=0 in 9
	replace ci_lower_controls_a2=0 in 9
	replace ci_upper_a2=0 in 9
	replace ci_lower_a2=0 in 9
	replace year=1999 in 9
	sort year
	
merge 1:1 year using `baseline', nogen
merge 1:1 year using `a1', nogen
	
*Figure comparing dummies with and without controls	
cd "$germanygraphics"

twoway (scatter dydx year, connect(direct) msize(small)) (scatter dydx_a1 year, connect(direct) msize(small)) (scatter dydx_a2 year, connect(direct) msize(small)) (rcap ci_upper ci_lower year, msize(0) lcolor(navy)) (rcap ci_upper_a1 ci_lower_a1 year, msize(0) lcolor(maroon)) (rcap ci_upper_a2 ci_lower_a2 year, msize(0) lcolor(dkgreen)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Baseline specification") label(2 "Alternative #1") label(3 "Alternative #2") order(1 2 3)) title("Marginal effects of year dummies on Pr(Insecure)" "Without controls") xtitle("Year") yline(0, lcolor(black*2)) ytitle("Marginal effect") saving(g1, replace)

twoway (scatter dydx_controls year, connect(direct) msize(small)) (scatter dydx_controls_a1 year, connect(direct) msize(small)) (scatter dydx_controls_a2 year, connect(direct) msize(small)) (rcap ci_upper_controls ci_lower_controls year, msize(0) lcolor(navy)) (rcap ci_upper_controls_a1 ci_lower_controls_a1 year, msize(0) lcolor(maroon)) (rcap ci_upper_controls_a2 ci_lower_controls_a2 year, msize(0) lcolor(dkgreen)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Baseline specification") label(2 "Alternative #1") label(3 "Alternative #2") order(1 2 3)) title("Marginal effects of year dummies on Pr(Insecure)" "With controls") xtitle("Year") yline(0, lcolor(black*2)) ytitle("Marginal effect") saving(g2, replace)

graph combine g1.gph g2.gph, cols(2) graphregion(color(white)) ysize(2) xsize(5.5)
graph export "soep_alternatives.png", as(png) replace

cd "$germanydata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}
